//
//  ThreeButtonTitleView.m
//  Tunnel
//
//  Created by wbb on 2025/7/6.
//
#import "ThreeButtonTitleView.h"

@interface ThreeButtonTitleView ()
{
    NSArray<NSString *> *_buttonTitles;
    UIColor *_selectedColor;
    UIColor *_unselectedColor;
    NSMutableArray<UIButton *> *_buttons;
    UIImageView *_indicatorView; // 选中指示器
}
@end

@implementation ThreeButtonTitleView


- (instancetype)initWithFrame:(CGRect)frame
                  buttonTitles:(NSArray<NSString *> *)titles
           selectedButtonColor:(UIColor *)selectedColor
         unselectedButtonColor:(UIColor *)unselectedColor {
    self = [super initWithFrame:frame];
    if (self) {
        _buttonTitles = titles;
        _selectedColor = selectedColor;
        _unselectedColor = unselectedColor;
        _buttons = [NSMutableArray array];
        _selectedIndex = 0;
        
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    // 确保至少有三个按钮
//    if (_buttonTitles.count < 3) {
//        _buttonTitles = @[@"按钮1", @"按钮2", @"按钮3"];
//    }
    
    // 计算每个按钮的宽度
    CGFloat buttonWidth = self.bounds.size.width / _buttonTitles.count * 1.f;
    
    // 创建三个按钮
    for (NSInteger i = 0; i < _buttonTitles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i * buttonWidth, 0, buttonWidth, self.bounds.size.height);
        [button setTitle:_buttonTitles[i] forState:UIControlStateNormal];
        [button setTitleColor:_unselectedColor forState:UIControlStateNormal];
        [button setTitleColor:_selectedColor forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        button.tag = i;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [_buttons addObject:button];
    }
    
    // 设置第一个按钮为选中状态
    _buttons[0].selected = YES;
    
    // 创建选中指示器
    _indicatorView = [[UIImageView alloc] initWithFrame:CGRectMake((buttonWidth -20)/2.f, self.bounds.size.height - 8, 20, 4)];
//    _indicatorView.backgroundColor = _selectedColor;
    _indicatorView.image = [UIImage imageNamed:@"gjtj_tab_icon"];
    [self addSubview:_indicatorView];
}

- (void)buttonClicked:(UIButton *)button {
    // 更新按钮状态
    _buttons[_selectedIndex].selected = NO;
    button.selected = YES;
    _selectedIndex = button.tag;
    
    // 移动指示器
    CGFloat buttonWidth = self.bounds.size.width / _buttonTitles.count * 1.f;
    [UIView animateWithDuration:0.3 animations:^{
        self->_indicatorView.frame = CGRectMake(button.tag * buttonWidth + (buttonWidth -20)/2.f, self.bounds.size.height - 8, 20, 4);
    }];
    
    // 回调代理
    if ([self.delegate respondsToSelector:@selector(threeButtonTitleView:didSelectButtonAtIndex:)]) {
        [self.delegate threeButtonTitleView:self didSelectButtonAtIndex:button.tag];
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    if (selectedIndex >= 0 && selectedIndex < _buttonTitles.count ) {
        _selectedIndex = selectedIndex;
        _buttons[_selectedIndex].selected = YES;
        
        CGFloat buttonWidth = self.bounds.size.width / _buttonTitles.count * 1.f;
        _indicatorView.frame = CGRectMake(selectedIndex * buttonWidth + (buttonWidth -20)/2.f, self.bounds.size.height - 8, 20, 4);
    }
}

@end
